package ttelectronic.mobileholder.core;

import java.util.ArrayList;

import ttelectronic.mobileholder.R;
import ttelectronic.mobileholder.common.MHCommon;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MHListDeviceActivity extends Activity {

	private static final int REQUEST_ENABLE_BT = 1;
	private BluetoothAdapter mBluetoothAdapter;
	private ArrayList<MHDeviceDetailObject> mListDeviceDiscovery;
	private MHListDeviceAdapter mListDeviceAdapter;
	private ListView mlvListDevice;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.activity_list_device);

		mListDeviceDiscovery = new ArrayList<MHDeviceDetailObject>();
		mListDeviceAdapter = new MHListDeviceAdapter(this, mListDeviceDiscovery);

		mlvListDevice = (ListView) findViewById(R.id.lvListDevice);
		mlvListDevice.setAdapter(mListDeviceAdapter);
		mlvListDevice.setOnItemClickListener(lvOnItemClickListener);

		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		checkBluetoothState();

		registerReceiver(actionFoundDevice, new IntentFilter(
				BluetoothDevice.ACTION_FOUND));

		mBluetoothAdapter.startDiscovery();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.list_device, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.optMenuScanningDevice:
			mListDeviceDiscovery.clear();
			mBluetoothAdapter.startDiscovery();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void checkBluetoothState() {
		if (mBluetoothAdapter == null)
			Log.d(MHCommon.TAG, "Bluetooth NOT support");
		else {
			if (mBluetoothAdapter.isEnabled()) {
				Log.d(MHCommon.TAG, "Bluetooth is Enabled.");
			} else {
				Log.d(MHCommon.TAG, "Bluetooth is NOT Enabled!");
				Intent enableBtIntent = new Intent(
						BluetoothAdapter.ACTION_REQUEST_ENABLE);
				startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
			}
		}
	}

	private final BroadcastReceiver actionFoundDevice = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				BluetoothDevice device = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				mListDeviceDiscovery.add(new MHDeviceDetailObject(device
						.getName(), device.getAddress(), device
						.getBluetoothClass().getDeviceClass()));
			}
			mListDeviceAdapter = new MHListDeviceAdapter(
					MHListDeviceActivity.this, mListDeviceDiscovery);
			mlvListDevice.setAdapter(mListDeviceAdapter);
		}
	};

	private OnItemClickListener lvOnItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			MHDeviceDetailObject deviceDetail = mListDeviceDiscovery
					.get(position);
			// Create the result Intent and include the MAC address
			Intent intent = new Intent();
			intent.putExtra(MHCommon.EXTRA_DEVICE_MAC_ADDRESS,
					deviceDetail.getDeviceMacAddress());
			// Set result and finish this Activity
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
	};
}
